local skynet = require "skynet"
local log = require "sky_log"
local def = require "game_base_def"
local skynet_db_tool = require "skynet_db_tool"
local machine = require "machine"

local handle_pb_room = {}

function handle_pb_room:test_req(agent, msg)
    log.debug("test_req msg=", msg)

    local ret = {}

    return ret 
end

--创建房间--后面想了下，选择了某个游戏还未产生房间ID时无须上报
function handle_pb_room:create_room(agent, msg)
    local ret = {}
    log.debug("---------------------------------------------------")

    --假定此次开的是初级房
    local game_level = 1 
    local game_id = skynet.getenv("game_id")
    local node_id = skynet.getenv("node_id")
    local cache_name = def.cache_key.room_index..game_id..":and game_level='1'"
    local ok1,  room_index = skynet_db_tool.get(cache_name, "room_index")
    log.debug("room_index=", room_index)

    local ok2,  max_index = skynet_db_tool.get(cache_name, "max_index")
    log.debug("max_index=", max_index)

    local ok3,  base_index = skynet_db_tool.get(cache_name, "base_index")
    log.debug("base_index=", base_index)

    room_index = tonumber(room_index) + 1
    if room_index > tonumber(max_index) then 
        room_index = base_index + 1
    end

    log.debug("cache_name=", cache_name)
    local ok4 = skynet_db_tool.set(cache_name, "room_index", room_index)
    
    local room_info = {}
    --服务器节点-游戏等级-房间序列号
    room_info.room_id =  node_id .."-".. game_level .."-" .. room_index
    room_info.room_id = "10001-1-100038"
    room_info.room_index = room_index
    room_info.room_name = "测试游戏初级房"
    room_info.config = {}
    log.debug("room_info =", room_info)
    
    --[[
    --后续做一个缓冲队列池
    local room = skynet.newservice("service_room")
    room_info.room_agent = room
    skynet.call(room, "lua", "create_room", agent, room_info)
    --]]

    room_info.player_list = {}
    table.insert(room_info.player_list, agent)
    skynet_db_tool.cache_set(room_info.room_id, room_info)
    machine:create_room(agent, room_info)

    local sql = "INSERT INTO room_detail (room_id, room_status, datetime) VALUES ( '"..room_info.room_id.."', 0, CURRENT_TIME());"
    skynet.call(".mysql", "lua", "call_sql", sql)
    log.debug("sql=", sql)
    log.debug("---------------------------------------------------")
    
    return ret
end

--加入房间
function handle_pb_room:join_room(agent, msg)
    local ret = {}
    local ok, room_info =  skynet_db_tool.cache_get("10001-1-100038")
    --skynet.call(info.room_agent, "lua", "join_room", agent, info)

    table.insert(room_info.player_list, agent)
    skynet_db_tool.cache_set(room_info.room_id, room_info)
    machine:join_room(agent, room_info)

    return ret
end

--离开房间
function handle_pb_room:leave_room(agent, msg)
    local ok, room_info =  skynet_db_tool.cache_get("10001-1-100038")
    --local ret = skynet.call(info.room_agent, "lua", "leave_room", agent, info)
    local ret

    for k, v in pairs(room_info.player_list) do
        if v==agent then 
            table.remove(room_info.player_list, k)
            break
        end
    end
    skynet_db_tool.cache_set(room_info.room_id, room_info)
    machine:leave_room(agent, room_info)

    return ret or {}
end

--解散房间
function handle_pb_room:dismiss_room(agent, msg)
    local ret = false
    local ok, info =  skynet_db_tool.cache_get("10001-1-100038")
    log.debug("leave_room info=", info)  
    --skynet.send(info.room_agent, "lua", "dissmiss_room", agent, info)
    machine:dissmiss_room(agent, msg)

    local sql = "UPDATE room_detail SET room_status=1 WHERE room_id=".."'"..info.room_id.."';"
    skynet.call(".mysql", "lua", "call_sql", sql)

    skynet_db_tool.set(def.cache_key.room_detail.."10001-1-100038", "detail_info", info)

    return ret
end

function handle_pb_room:start_game(agent, msg)
    local ret
    local ok, info =  skynet_db_tool.cache_get("10001-1-100038")
    --local ret = skynet.call(info.room_agent, "lua", "start_game", agent, info)
    machine:game_start(agent, msg)
    return ret or {}
end


return handle_pb_room